package com.example.amone.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.amone.R;
import com.example.amone.bean.MenuBean;

import java.util.List;

public class MenuAdapter extends RecyclerView.Adapter<MenuAdapter.MenuViewHolder> {

    private Context context;
    private List<MenuBean> menuBeanList;
    private View inflate;

    public MenuAdapter(Context context) {
        this.context = context;
    }

    @NonNull
    @Override
    public MenuViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        inflate = LayoutInflater.from(context).inflate(R.layout.item_all_menu, viewGroup, false);
        return new MenuViewHolder(inflate);
    }

    @Override
    public void onBindViewHolder(@NonNull MenuViewHolder menuViewHolder, int i) {
        if (menuViewHolder instanceof MenuViewHolder) {
            menuViewHolder.ivMenu.setImageResource(R.mipmap.ic_launcher);
            menuViewHolder.tvMenuName.setText(menuBeanList.get(i).getName());
            menuViewHolder.tvMenuPrice.setText(menuBeanList.get(i).getPrice() + "");
        }
        inflate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                listener.onMenuListener();
            }
        });
    }

    @Override
    public int getItemCount() {
        return menuBeanList == null ? 0 : menuBeanList.size();
    }

    public void setMenuList(List<MenuBean> menuBeanList) {
        this.menuBeanList = menuBeanList;
    }

    class MenuViewHolder extends RecyclerView.ViewHolder {
        ImageView ivMenu;
        TextView tvMenuName, tvMenuPrice;

        public MenuViewHolder(@NonNull View itemView) {
            super(itemView);
            ivMenu = itemView.findViewById(R.id.iv_menu);
            tvMenuName = itemView.findViewById(R.id.tv_menu_name);
            tvMenuPrice = itemView.findViewById(R.id.tv_menu_price);
        }
    }

    private OnMenuItemListener listener;

    public void setOnMenuItem(OnMenuItemListener listener) {
        this.listener = listener;
    }

    public interface OnMenuItemListener {
        void onMenuListener();
    }
}
